SWEPClasses = SWEPClasses or {}

SWEP = {}
SWEP.ClassName = "base"

AddCSLuaFile( "weapons/base.lua" )
include( "weapons/base.lua" )

SWEPClasses[ "base" ] = SWEP
SWEP = nil

local files = file.FindInLua("melonracer/gamemode/weapons/*.lua")

for _,val in pairs( files ) do
	local start,endpos,filename = val:find( "([%w_]*)\.lua" )
	
	if filename == "base" then continue end
	
	SWEP = table.Copy( SWEPClasses[ "base" ] )
	SWEP.BaseClass = SWEPClasses[ "base" ]
	SWEP.ClassName = filename
	
	AddCSLuaFile( "weapons/"..val )
	include( "weapons/"..val )
	
	SWEPClasses[ filename ] = SWEPClasses[ filename ] or {}
	table.CopyFromTo(SWEP, SWEPClasses[ filename ])
	SWEP = nil
end

function getScriptedWeapon( str )
	return SWEPClasses[ str ]
end

if SERVER then return end

syncvar.SetCallbackEntity( "weapon", function( pl, weapon )

	if pl:GetWeapon() then
		pl:GetWeapon():Holster()
	end
	
	local wep_class = getScriptedWeapon( weapon )
	if wep_class then
		pl.m_wCurWeapon = wep_class:Create()
		pl.m_wCurWeapon.Owner = pl
		pl.WorldModel = pl.WorldModel or ClientsideModel( "models/props_phx/construct/concrete_barrier00.mdl" )
		pl.WorldModel:SetModel( pl.m_wCurWeapon.WorldModel )
		pl.m_wCurWeapon:Initialize()
	else
		pl.m_wCurWeapon = nil
	end

end )
